class PokerStack {
    constructor(element, pokerList = []) {
        this.pokerList = pokerList
        this.length = this.pokerList.length
        this.el = element
        this.top = this.el.offsetTop
        this.left = this.el.offsetLeft
    }
    strictPush(...pokers) {
        if (this.canPush(pokers[0])) {
            this.push(...pokers)
        }
    }
    push(...pokers) {
        for (let i = 0; i < pokers.length; i++) {
            let p = pokers[i]
            this.pokerList.push(p)
            this.el.appendChild(p.el)
            let stack = p.currentStack
            if (stack !== null) {
                stack.delete(p)
            }
            p.currentStack = this
        }
        let l = pokers.length
        this.length += l
        return this.length
    }
    delete(poker) {
        let n = this.indexOf(poker)
        this.pokerList.splice(n, 1)
        this.length -= 1
    }
    pop() {
        this.length -= 1
        let p = this.pokerList.pop()
        p.currentStack = null
        return p
    }
    lastPoker() {
        if (this.length !== 0) {
            // log('this.length', this.length)
            // log('poker', this.pokerList[this.length - 1])
            // log('pokerList', this.pokerList)
            return this.pokerList[this.length - 1]
        }
        return null
    }
    multiPop(n) {
        let result = []
        for (let i = 0; i < n; i++) {
            let p = this.pop()
            result.push(p)
        }
        return result
    }
    splice(n) {
        let s = this.pokerList.splice(n)
        for (let i = 0; i < s.length; i++) {
            let p = s[i]
            p.currentStack = null
        }
        this.length -= s.length
        return s
    }
    canPush(poker) {
        // return true
        if (poker === null) {
            return false
        }
        let { number, color } = poker
        // log('number', number)
        let length = this.pokerList.length
        if (length === 0 && number === 13) {
            return true
        }
        if (length !== 0) {
            let n = length - 1
            let lastPoker = this.pokerList[n]
            if (number === lastPoker.number - 1 && color !== lastPoker.color) {
                return true
            }
        }
        return false
    }
    canPushStrict(poker) {
        // return true
        if (poker === null) {
            return false
        }
        let { number, suit } = poker
        let length = this.pokerList.length
        if (length === 0 && number === 1) {
            return true
        }
        if (length !== 0) {
            let n = length - 1
            let lastPoker = this.pokerList[n]
            if (number === lastPoker.number + 1 && suit === lastPoker.suit) {
                return true
            }
        }
        return false
    }

    indexOf(poker) {
        let index = -1
        let el = poker.el
        for (let i = 0; i < this.pokerList.length; i++) {
            if (el === this.pokerList[i].el) {
                index = i
                break
            }
        }
        return index
    }
    findPoker(element) {
        for (let i = 0; i < this.pokerList.length; i++) {
            let poker = this.pokerList[i]
            if (element === poker.el) {
                return poker
            }
        }
        return null
    }
    nextPoker(poker) {
        let n = this.indexOf(poker)
        if (n > -1 && n < this.length - 1) {
            return this.pokerList[n + 1]
        } else {
            return null
        }
    }
    nextSiblingPokers(poker) {
        let pokers = []
        let n = this.indexOf(poker)
        for (let i = n; i < this.length; i++) {
            let p = this.pokerList[i]
            pokers.push(p)
        }
        return pokers
    }
    highLightPokers(poker) {
        let n = this.indexOf(poker)
        for (let i = n; i < this.length; i++) {
            let p = this.pokerList[i]
            p.highLight()
            p.quick()
        }
    }
    blurPokers(poker) {
        if (poker === null) {
            return
        }
        let n = this.indexOf(poker)
        // log('n', n)
        if (n === -1) {
            return
        }
        for (let i = n; i < this.length; i++) {
            let p = this.pokerList[i]
            // log('this.length', this.length)
            // log('blurPokers stack', this.pokerList)
            // log('p', p)
            p.blur()
        }
    }
    coverAllChildrenPokers() {
        for (let i = 0; i < this.length; i++) {
            let p = this.pokerList[i]
            p.cover()
        }
    }
    setBoxShadow(length = 24) {
        let l =  5 / length * this.length
        this.el.style.boxShadow = `${-l}px ${-l}px 0 0px grey`
    }
    firstOpenedPoker() {
        let p = null
        for (let i = 0; i < this.length; i++) {
            let list = this.pokerList
            if (list[i].opened === true) {
                p = list[i]
                break
            }
        }
        return p
    }
    twinkle() {
        this.el.classList.add('twinkle')
        setTimeout(() => {
            this.el.classList.remove('twinkle')
        }, 900)
    }
    removeTwinkle() {
        this.el.classList.remove('twinkle')
    }
    canWin() {
        for (let i = 2; i < this.length; i++) {
            let count = 0
            let ps = []
            for (let j = 0; j < i; j++) {
                let p1 = this.pokerList[i]
                let p2 = this.pokerList[j]
                if (p1.number + 1 === p2.number && p1.color !== p2.color && p2.opened === false) {
                    ps.push(p2)
                    count += 1
                }
                if (count === 2) {
                    log(`不能赢，因为 ${ps[0].suit}_${ps[0].number} 和 ${ps[1].suit}_${ps[1].number} 在 ${p1.suit}_${p1.number} 下面`)
                    return false
                }
            }
        }
        return true
    }
}